/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import item.Item;
import myJPA.Model;

/**
 *
 * @author nacerkhalil
 */
public class DeleteTitle extends javax.swing.JPanel {

    /**
     * Creates new form DeleteTitle
     */
    public DeleteTitle() {
        initComponents();
        resultNotFoundLabel.setVisible(false);
        searchResultPanel.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        searchResultPanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        deleteButton = new javax.swing.JButton();
        searchButton = new javax.swing.JButton();
        resultNotFoundLabel = new javax.swing.JLabel();

        jLabel1.setText("Enter movie or video game ID:");

        searchResultPanel.setEnabled(false);

        jLabel2.setText("Title:");

        jTextField2.setEnabled(false);

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout searchResultPanelLayout = new org.jdesktop.layout.GroupLayout(searchResultPanel);
        searchResultPanel.setLayout(searchResultPanelLayout);
        searchResultPanelLayout.setHorizontalGroup(
            searchResultPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchResultPanelLayout.createSequentialGroup()
                .add(searchResultPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(searchResultPanelLayout.createSequentialGroup()
                        .add(152, 152, 152)
                        .add(jLabel2)
                        .add(28, 28, 28)
                        .add(jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, searchResultPanelLayout.createSequentialGroup()
                        .addContainerGap(330, Short.MAX_VALUE)
                        .add(deleteButton)))
                .addContainerGap())
        );
        searchResultPanelLayout.setVerticalGroup(
            searchResultPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(searchResultPanelLayout.createSequentialGroup()
                .add(15, 15, 15)
                .add(searchResultPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(deleteButton)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        searchButton.setText("Search");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        resultNotFoundLabel.setForeground(new java.awt.Color(255, 0, 0));
        resultNotFoundLabel.setText("No Item is associated with the ID");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(56, 56, 56)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(searchResultPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(resultNotFoundLabel)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(searchButton)
                .addContainerGap(219, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(35, 35, 35)
                .add(resultNotFoundLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchButton))
                .add(18, 18, 18)
                .add(searchResultPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(274, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
        try
        {
            resultNotFoundLabel.setVisible(false);
            searchResultPanel.setVisible(false);
            int id = Integer.parseInt(jTextField1.getText());
            item = Model.getEntityManager().find(Item.class, id);
            
            jTextField2.setText(item.getTitle());
            searchResultPanel.setVisible(true);
            jTextField1.setText("");
        }
        catch(Exception ex)
        {
            item = null;
            resultNotFoundLabel.setVisible(true);
            searchResultPanel.setVisible(false);
        }
    }//GEN-LAST:event_searchButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        try
        {
            if(item != null)
            {
                item.remove();
                item = null;
                searchResultPanel.setVisible(false);
                jTextField1.setText("");
            }
            else
                throw new Exception();
        }
        catch(Exception ex)
        {
            
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

//GEN-FIRST:event_addCopiesActionPerformed
 
//GEN-LAST:event_addCopiesActionPerformed
    
    private Item item;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JLabel resultNotFoundLabel;
    private javax.swing.JButton searchButton;
    private javax.swing.JPanel searchResultPanel;
    // End of variables declaration//GEN-END:variables
}
